// Package main provides ...
package main

import "fmt"

func main() {
	slice := []int{1, 2, 3, 4}
	slice = append(slice, 888)
	slice = append(slice, 888)
	slice = append(slice, 888)
	slice = append(slice, 888)
	slice = append(slice, 888)
	fmt.Println("slice=", slice)

	test1()
	test2()
	noSame()
}

/**
练习:给定一个字符串列表，在原有的slice上返回不包含字符串的列表
**/

func test1() {
	old := []string{"red", "", "blue", "", "", "pink"}
	new := make([]string, 0) //new :=[:0]

	for _, v := range old {
		if v != "" {
			fmt.Println("v=", v)
			new = append(new, v)
		}
	}
	fmt.Println("new=", new)
	fmt.Printf("new=%v,len(new)=%d,cap(new)=%d\n", new, len(new), cap(new))
}
func test2() {

	old := []string{"red", "", "blue", "", "", "pink"}
	i := 0
	for _, v := range old {
		if v != "" {
			old[i] = v
			i++
		}
	}
	old = old[:i]
	fmt.Println("old=", old)
	fmt.Printf("old=%v,len(old)=%d,cap(old)=%d\n", old, len(old), cap(old))
}

/**
练习二，写一个函数，消除[]string 中重复字符串
**/

func noSame() {
	data := []string{"red", "red", "blue", "blue", "black", "pink"}
	new := data[:1]
	for _, v := range data {
		flag := false
		for i := 0; i < len(new); i++ {
			if v == new[i] {
				flag = true
				break
			}
		}
		if !flag {
			new = append(new, v)
		}
	}
	fmt.Printf("data = %v\n", new)
}
